package com.silverhaze.diamond.client.ui.table;

import com.silverhaze.commons.helpers.swing.TableHelper;
import com.silverhaze.diamond.common.entity.Detail;
import com.silverhaze.diamond.client.ui.dialogs.DetailDialog;
import com.silverhaze.diamond.client.ui.table.model.CountedDetailsTableModel;

import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Comparator;

/**
 * SystemUser: Babanin
 * Date: 10.07.2008
 * Time: 23:23:32
 */
public class CountedDetailTable extends AbstractServiceTable {
    public CountedDetailTable() {
        this(new CountedDetailsTableModel());
    }

    public CountedDetailTable(CountedDetailsTableModel model) {
        super(model);

        init();
    }

    private void init() {
        setAutoCreateRowSorter(true);
        ((TableRowSorter) getRowSorter()).setComparator(1, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                return ((Long) o1).compareTo((Long) o2);
            }
        });

        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 2) {
                    final int row = getSelectedRow();
                    Detail detail = ((CountedDetailsTableModel) getModel()).getObjectAtRow(row).getDetail();

                    if (detail != null) {
                        DetailDialog.viewDetail(detail);
                    }
                }
            }
        });

        TableHelper.setFixedColumnSize(this, 1, 75);
    }

    @Override
    public void setModel(TableModel dataModel) {
        super.setModel(dataModel);

        TableHelper.setFixedColumnSize(this, 1, 75);
    }
}

